
import { ElMessage } from 'element-plus'
/**
* 消息提示框
* @param {Object} vm
* @param {String} text 提示信息
*/
export function messagePopup(text: string, msgtype?: 'success' | 'warning' | 'info' | 'error', dur?: number) { 
  ElMessage({
    message: text,
    duration: dur || 1500, // 0 不关闭弹窗
    center: true,
    type: msgtype || 'info',
    customClass: 'wm_message_tips'
  });
}



/**
 * 重置body字体大小
 */
 export function setDomFontSize(type?: number) { 
  let docEl = document.documentElement
  let uiw = 1920

  let resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
  let recalc = function () {
    // 以高度计算
    if(type ==  2) {
      var clientHeight = docEl.clientHeight;
      var uih = 1080
      if(clientHeight < 730) {
        uih = 1100
      }
      if (!clientHeight) return;
      docEl.style.fontSize = 100 * (clientHeight / uih) + 'px';
    }else {
      var clientWidth = docEl.clientWidth;
      if(clientWidth <= 768) {
        uiw = 750
      }
      if (!clientWidth) return;
      docEl.style.fontSize = 100 * (clientWidth / uiw) + 'px';
    }
  }
  if (!document.addEventListener) return;
  window.addEventListener(resizeEvt, recalc, false);
  document.addEventListener('DOMContentLoaded', recalc, false);
  recalc()
  window.addEventListener('resize', function () {
    recalc()
  })
}

/*
* 获取地址栏参数
* @param {string} name
*/
export function getUrlParms(name: string): string | null  {
 const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
   const r = window.location.search.substr(1).match(reg) || window.location.hash.substring((window.location.hash.search(/\?/)) + 1).match(reg);
   if (r != null) {
     return decodeURIComponent(r[2]);
   }
   return null
}


/**
 * 设置Cookie
 * @param {string} key
 * @param {*} val
 * @param {*} path
 */
 export function setCookie(key: string, val: string, path?: string) {
  if (!path) path = "/";
  document.cookie = key + "=" + val + "; expires=Session; path=" + path;  //设置cookie
}

/**
 * 获取Cookie
 * @param {string} key 获取 Cookie
 */
export function getCookie(key: string): string {
  /*获取cookie参数*/
  let getCookie = document.cookie.replace(/[ ]/g, "");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
  let arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
  let tips: string  = '';  //声明变量tips
  for (let i = 0; i < arrCookie.length; i++) {   //使用for循环查找cookie中的tips变量
    let arr = arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
    if (key == arr[0]) {  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
      tips = arr[1];   //将cookie的值赋给变量tips
      break;   //终止for循环遍历
    }
  }
  return tips;
}
import { ElLoading } from 'element-plus'
export function loadingText(text?: string, className?: any) {
  let loading = ElLoading.service({
    text: text || '拼命加载中...',
    spinner: 'el-icon-loading',
    customClass: 'loading_wait',
    background: 'rgba(0, 0, 0, 0.5)', 
    target: className ? document.querySelector(className) : document.body
  });
  return loading
}

export function clearCookies()  {
  sessionStorage.clear()
}



const KEY_MAP = {
  'AQI2': 'AQI',
  'IAQI': 'I·AQI',
  'PM25': `PM<sub>2.5</sub>`,
  'CO2': `CO<sub>2</sub>`,
  'PM10': `PM<sub>10</sub>`,
  'SO2': `SO<sub>2</sub>`,
  'NO2': `NO<sub>2</sub>`,
  'O3': `O<sub>3</sub>`,
  'TEM': '温度',
  'HUM': '湿度',
  'PM25_24H': 'PM<sub>2.5</sub>_24h',
  'PM10_24H': 'PM<sub>10</sub>_24h',
  'O3_8H': 'O<sub>3</sub>_8h',
  'O3_8H(MAX)': 'O<sub>3</sub>_8h(Max)', 
  'PM2.5浓度均值': 'PM<sub>2.5</sub>浓度均值',
  'PM25/PM10': 'PM<sub>2.5</sub>/PM<sub>10</sub>',
  'NO3-/SO42-': 'NO<sub>3</sub><sup>-</sup>/SO<sub>4</sub><sup>2-</sup>',
  'NO3-': 'NO<sub>3</sub><sup>-</sup>',
  'SO42-': 'SO<sub>4</sub><sup>2-</sup>',
  'NH4+': 'NH<sub>4</sub>',
  'CL-': 'CL<sup>-</sup>',
  'NA_': 'NA<sup>+</sup>',
  'K+': 'K<sup>+</sup>',
  'MG2+': 'MG<sup>2+</sup>',
  'CA2+': 'Ca<sup>2+</sup>',
  'VIRUS': '生物粒子总数',
  'HCHO': '甲醛', 
}

/**
 * 转换污染源下角标
 * @param name 污染源name
 * @returns {*}
 */
export function formatterSub(name: string) {
  let key = name.toUpperCase()
  let html = KEY_MAP[key]
  if (html) {
    return html
  }
  return name
}

export const LEVEL_COLOR = {
  '0': '#FFB700',
  '1': '#30BF78',
  '2': '#1D84EF',
  '3': '#C24202',
  '4a': '#FF2800',
  '4b': '#9F259D'
}
export function getLevelColor(lavel: string) {
  
}